let StorageEs = {}
/* -----------------cookie--------------------- */
/**
 * 设置cookie
 * @param {*} name
 * @param {*} value
 * @param {*} day
 */
StorageEs.setCookie = function (name, value, day) {
    var setting = arguments[0]
    if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object') {
        for (var i in setting) {
            let oDate = new Date()
            oDate.setDate(oDate.getDate() + day)
            document.cookie = i + '=' + setting[i] + ';expires=' + oDate
        }
    } else {
        let oDate = new Date()
        oDate.setDate(oDate.getDate() + day)
        document.cookie = name + '=' + value + ';expires=' + oDate
    }
}

/**
 * 获取cookie
 * @param {*} name
 */
StorageEs.getCookie = function (name) {
    var arr = document.cookie.split('; ')
    for (var i = 0; i < arr.length; i++) {
        var arr2 = arr[i].split('=')
        if (arr2[0] === name) {
            return arr2[1]
        }
    }
    return ''
}

/**
 * 删除cookie
 * @param {*} name
 */
StorageEs.removeCookie = function (name) {
    this.setCookie(name, 1, -1)
}

/* -----------------localStorage--------------------- */
/**
 * 设置localStorage
 * @param {*} key
 * @param {*} val
 */
StorageEs.setLocal = function (key, val) {
    var setting = arguments[0]
    if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object') {
        for (var i in setting) {
            localStorage.setItem(i, JSON.stringify(setting[i]))
        }
    } else {
        localStorage.setItem(key, JSON.stringify(val))
    }
}

/**
 * 获取localStorage
 * @param {*} key
 */
StorageEs.getLocal = function (key) {
    if (key) return JSON.parse(localStorage.getItem(key))
    return null
}

/**
 * 移除localStorage
 * @param {*} key
 */
StorageEs.removeLocal = function (key) {
    localStorage.removeItem(key)
}

/**
 * 移除所有localStorage
 */
StorageEs.clearLocal = function () {
    localStorage.clear()
}

/* -----------------sessionStorage--------------------- */
/**
 * 设置sessionStorage
 * @param {*} key
 * @param {*} val
 */
StorageEs.setSession = function (key, val) {
    var setting = arguments[0]
    if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object') {
        for (var i in setting) {
            sessionStorage.setItem(i, JSON.stringify(setting[i]))
        }
    } else {
        sessionStorage.setItem(key, JSON.stringify(val))
    }
}

/**
 * 获取sessionStorage
 * @param {*} key
 */
StorageEs.getSession = function (key) {
    if (key) return JSON.parse(sessionStorage.getItem(key))
    return null
}

/**
 * 移除sessionStorage
 * @param {*} key
 */
StorageEs.removeSession = function (key) {
    sessionStorage.removeItem(key)
}

/**
 * 移除所有sessionStorage
 */
StorageEs.clearSession = function () {
    sessionStorage.clear()
}

export default StorageEs
